﻿#region license
// Copyright (C) 2020 ClassicUO Development Community on Github
// 
// This project is an alternative client for the game Ultima Online.
// The goal of this is to develop a lightweight client considering
// new technologies.
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#endregion

using System.Collections.Generic;
using System.Reflection;

using ClassicUO.Game.GameObjects;

namespace ClassicUO.Utility
{
    internal static class ReflectionHolder
    {
        public static Dictionary<string, string> GetGameObjectProperties<T>(T obj) where T : GameObject
        {
            PropertyInfo[] props = obj?.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            FieldInfo[] fields = obj?.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);

            Dictionary<string, string> dict = new Dictionary<string, string>();

            if (props != null)
            {
                foreach (PropertyInfo prop in props)
                {
                    if (prop.PropertyType.IsByRef)
                    {
                    }
                    else
                    {
                        object value = prop.GetValue(obj, null);

                        dict[prop.Name] = value == null ? "null" : value.ToString();
                    }
                }
            }

            if (fields != null)
            {
                foreach (var prop in fields)
                {
                    if (prop.FieldType.IsByRef)
                    {
                    }
                    else
                    {
                        object value = prop.GetValue(obj);

                        dict[prop.Name] = value == null ? "null" : value.ToString();
                    }
                }
            }

            return dict;
        }
    }
}